package com.un.ebs.context.service.impl;

import com.un.ebs.context.service.TokenService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author zc
 * @ClassName TokenServiceImpl
 * @Description
 * @Date 2021/3/31
 */
@Service(value = "tokenService")
public class TokenServiceImpl implements TokenService {
    @Autowired
    RedissonClient redissonClient;

    @Override
    public <T> void set(String key, T value, Long sec) {
        RBucket<T> bucket = redissonClient.getBucket(key);
        if (sec != 0) {
            bucket.set(value, sec, TimeUnit.SECONDS);
        } else {
            bucket.set(value);
        }
    }

    @Override
    public <T> T get(String key) {
        RBucket<T> bucket = redissonClient.getBucket(key);
        return bucket.get();
    }
}
